package cn.source.system.controller;

import cn.source.common.annotation.Log;
import cn.source.common.constant.HttpStatus;
import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.enums.BusinessType;
import cn.source.common.utils.StringUtils;
import cn.source.system.domain.NavSiteConfig;
import cn.source.system.service.INavSiteConfigService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @program: source-vue
 * @ClassName: NavSiteConfigController
 * @description: 网站配置Controller
 * @author: 诗涵
 * @create: 2024-02-20 14:36
 */
@RestController
@RequestMapping("/nav/config")
@AllArgsConstructor
public class NavSiteConfigController  extends BaseController {

    private final INavSiteConfigService navSiteConfigService;

    /**
     * 获取配置
     * @return
     */
    @GetMapping("/getConfig")
    @PreAuthorize("@ss.hasPermi('nav:config:get')")
    public AjaxResult getConfig() {
        List<NavSiteConfig> list = navSiteConfigService.selectNavSiteConfigList(new NavSiteConfig());
        return new AjaxResult(HttpStatus.SUCCESS,"获取配置成功",list.size()>0? list.get(0) : null);
    }

    /**
     * 网站配置
     * @param navSiteConfig
     * @return
     */
    @PutMapping
    @Log(title = "网站配置", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('nav:config:update')")
    public AjaxResult update(@RequestBody NavSiteConfig navSiteConfig) {
        String id = String.valueOf(navSiteConfig.getId());
        if(StringUtils.isEmpty(id)){
            return toAjax(navSiteConfigService.insertNavSiteConfig(navSiteConfig));
        }else{
            return toAjax(navSiteConfigService.updateNavSiteConfig(navSiteConfig));
        }
    }
}
